﻿package
{
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;

	public class DrawRoad extends Sprite implements IGetRoadArr
	{

		//==========================================================================
		//		Constants
		//==========================================================================	
		//==========================================================================
		//		Constructor
		//==========================================================================
		public function DrawRoad(st:DisplayObjectContainer):void
		{
			nowStage=st;
			super();
			init();
		}		
		//==========================================================================
		//		Valuables
		//==========================================================================	
		private var isDrawing:Boolean;
		private var isDown:Boolean;
		private var nowX,nowY:Number;
		private var tempX,tempY:Number
		private var roadArr:Array;
		private var nowStage:DisplayObjectContainer;
		private var tempContainer:Sprite;	
		private var _color:int;
		//==========================================================================
		//		Properties
		//==========================================================================	
		//------------------------------
		//		abc
		//------------------------------	
		//==========================================================================
		//		Public Methods
		//==========================================================================
		public function set color(c:int):void{
			_color=c;
		}
		public function get color():int{
			return _color;
		}
		public function getRoadArr():Array
		{
			return roadArr;
		}
		public function clear():void
		{
			this.graphics.clear();
			roadArr=new Array();
			isDrawing=false;
			isDown=false;
			nowStage.removeEventListener(MouseEvent.ROLL_OVER,overHandler);
			nowStage.addEventListener(MouseEvent.MOUSE_DOWN,downHandler);
		}		
		//==========================================================================
		//		Private Methods
		//==========================================================================
		private function init():void
		{
			this.nowStage.addChild(this);
			roadArr = new Array();
			tempContainer=new Sprite();
			this.addChild(tempContainer);
			nowStage.addEventListener(MouseEvent.MOUSE_DOWN, downHandler);
			_color=0X555555;
		}
		private function downHandler(evt:MouseEvent):void
		{
			nowStage.removeEventListener(MouseEvent.MOUSE_DOWN, downHandler);
			nowStage.addEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
			nowStage.addEventListener(MouseEvent.MOUSE_UP, upHandler);
			//clear();
			if(isDrawing)
			{
				graphics.lineStyle(2,_color);
				graphics.moveTo(nowX,nowY);
				graphics.lineTo(tempX,tempY);
			}
			//将第一点坐标放入_roadArr数组；
			nowX=this.mouseX;
			nowY=this.mouseY;
			var tempPoint:Point= new Point(nowX,nowY);
			roadArr.push(tempPoint);
			isDrawing=true;
			isDown=true;
		}
		private function moveHandler(evt:MouseEvent):void
		{
			if(isDown)
			{
				//绘制曲线
				var x,y:Number;
				x=this.mouseX;
				y=this.mouseY;
				graphics.lineStyle(2,_color);
				graphics.moveTo(nowX,nowY);
				graphics.lineTo(x,y);
				nowX=x;
				nowY=y;
				//将移动的点坐标放入_roadArr数组；
				var tempPoint:Point= new Point(nowX,nowY);
				roadArr.push(tempPoint);
			}else{
				tempContainer.graphics.clear();
				tempX=this.mouseX;
				tempY=this.mouseY;
				tempContainer.graphics.lineStyle(0,0x550000,.5);
				tempContainer.graphics.moveTo(nowX,nowY);
				tempContainer.graphics.lineTo(tempX,tempY);
			}	
		}
		private function upHandler(evt:MouseEvent):void
		{
			nowStage.addEventListener(MouseEvent.MOUSE_DOWN, downHandler);
			nowStage.addEventListener(MouseEvent.ROLL_OUT,outHandler);
			nowStage.removeEventListener(MouseEvent.MOUSE_UP, upHandler);
			isDown = false;
		}
		private function outHandler(evt:MouseEvent):void
		{
			tempContainer.graphics.clear();
			nowStage.removeEventListener(MouseEvent.ROLL_OUT,outHandler);
			nowStage.removeEventListener(MouseEvent.MOUSE_MOVE,moveHandler);
			nowStage.removeEventListener(MouseEvent.MOUSE_DOWN,downHandler);
			nowStage.addEventListener(MouseEvent.ROLL_OVER,overHandler);
		}
		private function overHandler(evt:MouseEvent):void
		{
			//trace("over");
			nowStage.removeEventListener(MouseEvent.ROLL_OVER,overHandler);
			nowStage.addEventListener(MouseEvent.MOUSE_MOVE,moveHandler);
			nowStage.addEventListener(MouseEvent.MOUSE_DOWN,downHandler);
			nowStage.addEventListener(MouseEvent.ROLL_OUT,outHandler);
		}
		//==========================================================================
		//		Destroy
		//==========================================================================

	}
}